package com.lxr.test5002;

import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.widget.Button;
import android.widget.GridView;
import com.lxr.test5002.adapter.PhotoAdapter;
import com.lxr.test5002.base.BaseActivity;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;

public class PhotoReviewAcitivty extends BaseActivity {
    @BindView(R.id.gv_photo_layout)
    GridView mGvPhotoLayout;
    private List<String> photonames;

    @BindView(R.id.btn_photo_result)
     Button mBtnSave;
    private PhotoAdapter mAdapter;
    public static final int REQUEST_PHOTO = 200;
    public static final String RESULT_KEY = "photoPath";
    private int mCameraId;

    public PhotoReviewAcitivty() {
        super(R.layout.activity_photo_review);
    }


    @Override
    protected void initView(Bundle savedInstanceState) {

    }


    @Override
    protected void initData() {
        mCameraId = getIntent().getIntExtra("cameraId", Camera.CameraInfo.CAMERA_FACING_BACK);
        photonames = new ArrayList<>();
        mAdapter = new PhotoAdapter(this, photonames);
        mGvPhotoLayout.setAdapter(mAdapter);
        Intent intent = new Intent(this, Camera2Activity.class);
        intent.putExtra("cameraId", mCameraId);
        startActivityForResult(intent, REQUEST_PHOTO);
    }

    @Override
    protected void initEvent() {
        mBtnSave.setOnClickListener(v -> {
            finish();
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_PHOTO) {
            if (resultCode == 101) {
                if (data != null) {
                    if (photonames != null) {
                        photonames.clear();
                        ArrayList<String> photos = data
                                .getStringArrayListExtra(PhotoReviewAcitivty.RESULT_KEY);
                        if (photos != null && photos.size() > 0) {
                            photonames.addAll(photos);
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                }
            } else {
                finish();
            }
        }
    }
}
